import { Injectable } from '@nestjs/common';
import { DbService } from '../../db/db.service';
import { FindOptionsWhere, LessThan } from 'typeorm';
import { WaterBills } from '@app/db/entity/WaterBills';

@Injectable()
export class WaterBillsService {
    constructor(
        private readonly db:DbService,
    ){}

    async page(query:Pagination, userId:number) {
        let where:FindOptionsWhere<WaterBills> = { userId }
        const lastId = query.lastKey || 0
        const pageSize = query.pageSize || 20

        if(lastId > 0) {
            where.id = LessThan(lastId)
        }

        let rows:WaterBills[] = await this.db.waterBills.getRepository().find({
            where,
            skip : 0, 
            take : pageSize, 
            order : {id : 'DESC'}
        })

        return rows
    }
}
